package com.tianxinweb.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.tianxinweb.entity.Privilege;
import com.tianxinweb.enums.ResultEnum;
import com.tianxinweb.exception.MyException;
import com.tianxinweb.mapper.PrivilegeMapper;
import com.tianxinweb.service.PrivilegeService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cboy
 * @since 2018-07-19
 */
@Service
public class PrivilegeServiceImpl extends ServiceImpl<PrivilegeMapper, Privilege> implements PrivilegeService {

    @Autowired
    private PrivilegeMapper privilegeMapper;

    @Override
    public int getMenuLevel() {
        Map<String, Object> map = selectMap(new EntityWrapper<Privilege>()
                .setSqlSelect("max(priv_level) level_")
                .eq("menu_status",true)
                .eq("show_status",true)
        );
        return map==null|| map.isEmpty()?-1: (int) map.get("level_");
    }

    @Override
    public List<Privilege> getMenuByLevel(int i) {
        return selectList(new EntityWrapper<Privilege>()
                .eq("menu_status",true)
                .eq("show_status",true)
                .eq("priv_level",i)
                .orderBy("sort",false));
    }

    @Override
    public List<Privilege> getManagerMenuByLevel(int i,List<Integer> idList) {
        return selectList(new EntityWrapper<Privilege>()
                .in("pid",idList)
                .eq("priv_level",i)
                .orderBy("sort",false));
    }

    @Override
    @Transactional
    public boolean addPrivilege(Privilege privilege) {

        Privilege dbPriv = new Privilege();
        dbPriv.setPrivName(privilege.getPrivName());
        dbPriv.setPrivDesc(privilege.getPrivDesc());
        dbPriv.setSort(privilege.getSort()==null?0:privilege.getSort());
        dbPriv.setPrivUrl(privilege.getPrivUrl()==null?"":privilege.getPrivUrl());

        if(privilege.getMenuStatus() != null){
            dbPriv.setIcon(privilege.getIcon()==null?"":privilege.getIcon());
            dbPriv.setMenuStatus(privilege.getMenuStatus());
            dbPriv.setShowStatus(privilege.getShowStatus()==null?true:privilege.getShowStatus());
        }

        //判断权限是否拥有上级
        if(privilege.getParentId() != null){
            Privilege parentPriv = selectById(privilege.getParentId());
            if(parentPriv!=null){
                //菜单的上级必须为菜单
                if(privilege.getMenuStatus() && !parentPriv.getMenuStatus()){
                    throw new MyException(ResultEnum.PARENT_PRIVILEGE_TYPE_ERROR);
                }
                dbPriv.setParentId(parentPriv.getPid());
                insert(dbPriv);//保存权限

                dbPriv.setPrivLevel(parentPriv.getPrivLevel()+1);
                dbPriv.setPrivPath(parentPriv.getPrivPath()+"-"+(dbPriv.getPid()+10000));//+1000的作用是避免排序时出现“2”>“100”的情况
                return updateById(dbPriv);//更改权限信息
            }
        }

        dbPriv.setPrivLevel(0);
        insert(dbPriv);
        dbPriv.setPrivPath((dbPriv.getPid()+10000)+"");
        return updateById(dbPriv);//更改权限信息
    }

    @Override
    @Transactional
    public boolean updatePrivilege(Privilege privilege) {
        Privilege dbPriv = selectById(privilege.getPid());
        if(dbPriv==null) throw new MyException(ResultEnum.PRIVILEGE_PARENT_ID_ERROR);

        dbPriv.setPrivName(privilege.getPrivName());
        dbPriv.setPrivDesc(privilege.getPrivDesc());
        dbPriv.setSort(privilege.getSort()==null?0:privilege.getSort());
        dbPriv.setPrivUrl(privilege.getPrivUrl()==null?"":privilege.getPrivUrl());

        if(privilege.getMenuStatus() != null){
            dbPriv.setIcon(privilege.getIcon()==null?"":privilege.getIcon());
            dbPriv.setMenuStatus(privilege.getMenuStatus()==null?false:privilege.getMenuStatus());
            dbPriv.setShowStatus(privilege.getShowStatus()==null?true:privilege.getShowStatus());
        }

        //判断权限是否拥有上级
        if(privilege.getParentId() != null){
            Privilege parentPriv = selectById(privilege.getParentId());
            if(parentPriv!=null){
                //菜单的上级必须为菜单
                if(privilege.getMenuStatus() !=null  && !parentPriv.getMenuStatus()){
                    throw new MyException(ResultEnum.PARENT_PRIVILEGE_TYPE_ERROR);
                }
                //校验父权限不能为自己的子权限
                if(parentPriv.getPrivPath().startsWith(dbPriv.getPrivPath()))
                    throw new MyException(ResultEnum.PARENT_PRIVILEGE_ERROR);
                dbPriv.setParentId(parentPriv.getPid());

                dbPriv.setPrivLevel(parentPriv.getPrivLevel()+1);
                dbPriv.setPrivPath(parentPriv.getPrivPath()+"-"+(dbPriv.getPid()+10000));
            }
        }

        return updateById(dbPriv);//更改权限信息
    }

    @Override
    public List<Privilege> getOrderedPrivilege(Integer type) {
        if(type==0){
            return selectList(new EntityWrapper<Privilege>().orderBy("priv_path",true));
        }else{
            return selectList(new EntityWrapper<Privilege>()
                    .eq("menu_status",true)
                    .orderBy("priv_path",true));
        }

    }

    @Override
    public int getPrivLevel() {
        Map<String, Object> map = selectMap(new EntityWrapper<Privilege>()
                .setSqlSelect("max(priv_level) level_")
        );
        return map==null|| map.isEmpty()?-1: (int) map.get("level_");
    }

    @Override
    public List<Privilege> getPrivByLevel(int i) {
        return selectList(new EntityWrapper<Privilege>()
                .eq("priv_level",i)
                .orderBy("sort",false));
    }
}
